package mbean;

import java.util.ArrayList;
import java.util.List;

import org.richfaces.component.html.HtmlTree;
import org.richfaces.component.state.TreeState;
import org.richfaces.event.NodeSelectedEvent;
import org.richfaces.model.TreeNode;
import org.richfaces.model.TreeNodeImpl;
import org.richfaces.model.TreeRowKey;

import util.FacesUtil;
import vo.ProcessVO;
import vo.TreeNodeVO;
import dao.ProcessDAO;

public class MenuMBean extends AbstractMBean{

	private String menuCode;
	private ProcessDAO dao;
	private TreeNode<TreeNodeVO> root;
	private TreeState treeState;

	public MenuMBean() {
		dao = new ProcessDAO();
		loadTree();
	}

	public void loadTree() {
		root = new TreeNodeImpl<TreeNodeVO>();
		
		List<ProcessVO> processos = dao.getProcess();
		StrategyMBean sp = (StrategyMBean) FacesUtil.getSessionMapValue("strategy");
		dao.verifyProcessCompleted(sp.getStrategicPlanId());
		
		int id = 1;
		
		TreeNode<TreeNodeVO> noPlano = createNode("plano", sp.getNome(), id);
		addNode(id, noPlano, this.root, "plano");
		id++;
		
		for (ProcessVO vo : processos) {
			TreeNode<TreeNodeVO> node = createNode(vo.getCode(), vo.getName(), vo.getId());
			
			for (ProcessVO avo : vo.getActivities()) {								
				TreeNode<TreeNodeVO> nodeFilho = createNode(avo.getCode(), avo.getName(), avo.getId());
				addNode(id, nodeFilho, node, avo.getCode());
				id++;
			}
			
			addNode(id, node, noPlano, vo.getCode());
			id++;
		}		
	}	

	private void addNode(int id, TreeNode<TreeNodeVO> noFilho,
			TreeNode<TreeNodeVO> noPai, String menuCode) {
		noPai.addChild(id, noFilho);
	}

	private TreeNode<TreeNodeVO> createNode(String code, String name, int id){
		TreeNodeVO vo = new TreeNodeVO();
		vo.setCode(code);
		vo.setName(name);
		vo.setChild(new ArrayList<TreeNodeVO>());
		vo.setId(id);
		vo.setOpened(false);
		
		if (dao.processCompleted(code) == 2){
			vo.setImg("/files/complete.png");
		}
		else if (dao.processCompleted(code) == 1){
			vo.setImg("/files/meioCompleto.png");
		}
		else{
			vo.setImg("/files/incomplete.png");
		}
		
		TreeNode<TreeNodeVO> node = new TreeNodeImpl<TreeNodeVO>();		
		node.setData(vo);
		
		return node;
	}

	public void setMenuCode(String menuCode) {
		this.menuCode = menuCode;
	}

	public String getMenuCode() {
		return menuCode;
	}

	public String redirecionaTela() {
		String retorno = "";
		
		if (this.menuCode.equalsIgnoreCase("survey")){
			SurveyMBean smb;
			
			if (FacesUtil.getSessionMapValue("survey") == null){
				smb = new SurveyMBean();
			} else{
				smb = (SurveyMBean) FacesUtil.getSessionMapValue("survey");
			}
			
			retorno = smb.open();
			FacesUtil.setSessionMapValue("survey", smb);
		} else if (this.menuCode.equalsIgnoreCase("definePerspectives")){
			BSCPerspectiveMBean bscpmb;

			if (FacesUtil.getSessionMapValue("bscPerspective") == null) {
				bscpmb = new BSCPerspectiveMBean();
			} else {
				bscpmb = (BSCPerspectiveMBean) FacesUtil.getSessionMapValue("bscPerspective");
			}

			retorno = bscpmb.open();
			FacesUtil.setSessionMapValue("bscPerspective", bscpmb);
		} else if (this.menuCode.equalsIgnoreCase("defineMission")){
			OrganizationMBean omb;
			
			if (FacesUtil.getSessionMapValue("organization") == null){
				omb = new OrganizationMBean();
			} else {
				omb = (OrganizationMBean) FacesUtil.getSessionMapValue("organization");
			}
			
			omb.open();
			omb.setMenuSaveDisabled(true);
			retorno = "defineMission";
			FacesUtil.setSessionMapValue("organization", omb);			
		} else if (this.menuCode.equalsIgnoreCase("swot")) {
			SWOTMBean smb;

			if (FacesUtil.getSessionMapValue("swot") == null) {
				smb = new SWOTMBean();
			} else {
				smb = (SWOTMBean) FacesUtil.getSessionMapValue("swot");
			}

			retorno = smb.open();
			FacesUtil.setSessionMapValue("swot", smb);
		} else if (this.menuCode.equalsIgnoreCase("establishGoals")) {
			BSCMbean bscmb;

			if (FacesUtil.getSessionMapValue("bsc") == null) {
				bscmb = new BSCMbean();
			} else {
				bscmb = (BSCMbean) FacesUtil.getSessionMapValue("bsc");
			}

			retorno = bscmb.open();
			FacesUtil.setSessionMapValue("bsc", bscmb);
		} else if (this.menuCode.equalsIgnoreCase("defineValues")) {
			OrganizationMBean omb;
			
			if (FacesUtil.getSessionMapValue("organization") == null){
				omb = new OrganizationMBean();
			} else {
				omb = (OrganizationMBean) FacesUtil.getSessionMapValue("organization");
			}
			
			omb.open();
			omb.setMenuSaveDisabled(false);
			retorno = "defineValues";
			FacesUtil.setSessionMapValue("organization", omb);
		} else if (this.menuCode.equalsIgnoreCase("establishVision")) {
			OrganizationMBean omb;
			
			if (FacesUtil.getSessionMapValue("organization") == null){
				omb = new OrganizationMBean();
			} else {
				omb = (OrganizationMBean) FacesUtil.getSessionMapValue("organization");
			}
			
			omb.open();
			omb.setMenuSaveDisabled(true);
			retorno = "establishVision";
			FacesUtil.setSessionMapValue("organization", omb);
		}
		else if (this.menuCode.equalsIgnoreCase("createStrategyMap")){
			retorno = "createStrategyMap";
		}
		else if (this.menuCode.equalsIgnoreCase("plano")){
			setaTela(true, "strategy");
			retorno = "plano";
		}
		else if (this.menuCode.equalsIgnoreCase("inicio")){
			setaTela(false, "");
			retorno = "start";
			FacesUtil.addErrorMessage(FacesUtil.getMessageResourceString("msgApoioInicio1"));
			FacesUtil.addErrorMessage(FacesUtil.getMessageResourceString("msgApoioInicio2"));
		}
		else if (this.menuCode.equalsIgnoreCase("defineStrategicMembers")){
			setaTela(false, "");
			retorno = "defineStrategicMembers";
			FacesUtil.addErrorMessage(FacesUtil.getMessageResourceString("msgFuncionalidadeNaoImplementada"));
		}
		else if (this.menuCode.equalsIgnoreCase("establishStrategicFoundations")){
			setaTela(false, "");
			retorno = "establishStrategicFoundations";
			FacesUtil.addErrorMessage(FacesUtil.getMessageResourceString("msgApoioEstablishStrategicFoundations"));
		}
		else if (this.menuCode.equalsIgnoreCase("gatherIntelligencePlanning")){
			setaTela(false, "");
			retorno = "gatherIntelligencePlanning";
			FacesUtil.addErrorMessage(FacesUtil.getMessageResourceString("msgApoioRecolherInformacoes1"));
			FacesUtil.addErrorMessage(FacesUtil.getMessageResourceString("msgApoioRecolherInformacoes2"));
			FacesUtil.addErrorMessage(FacesUtil.getMessageResourceString("msgApoioRecolherInformacoes3"));
		}
		else if (this.menuCode.equalsIgnoreCase("analiseAmbiente")){
			setaTela(false, "");
			retorno = "analiseAmbiente";
			FacesUtil.addErrorMessage(FacesUtil.getMessageResourceString("msgFuncionalidadeNaoImplementada"));
		}
		else if (this.menuCode.equalsIgnoreCase("stakeholders")){
			setaTela(false, "");
			retorno = "stakeholders";
			FacesUtil.addErrorMessage(FacesUtil.getMessageResourceString("msgFuncionalidadeNaoImplementada"));
		}
		else if (this.menuCode.equalsIgnoreCase("formulateStrategy")){
			setaTela(false, "");
			retorno = "formulateStrategy";
			FacesUtil.addErrorMessage(FacesUtil.getMessageResourceString("msgFuncionalidadeNaoImplementada"));
		}
		else if (this.menuCode.equalsIgnoreCase("createBSC")){
			setaTela(false, "");
			retorno = "createBSC";
			FacesUtil.addErrorMessage(FacesUtil.getMessageResourceString("msgApoioBSC"));
		}
		else if (this.menuCode.equalsIgnoreCase("chooseGoalsMetrics")){
			setaTela(false, "");
			retorno = "chooseGoalsMetrics";
			FacesUtil.addErrorMessage(FacesUtil.getMessageResourceString("msgFuncionalidadeNaoImplementada"));
		}
		else if (this.menuCode.equalsIgnoreCase("buildCauseEffectLinks")){
			setaTela(false, "");
			retorno = "buildCauseEffectLinks";
			FacesUtil.addErrorMessage(FacesUtil.getMessageResourceString("msgFuncionalidadeNaoImplementada"));
		}
		else if (this.menuCode.equalsIgnoreCase("establishTargets")){
			setaTela(false, "");
			retorno = "establishTargets";
			FacesUtil.addErrorMessage(FacesUtil.getMessageResourceString("msgFuncionalidadeNaoImplementada"));
		}
		else if (this.menuCode.equalsIgnoreCase("designateGoalsManagers")){
			setaTela(false, "");
			retorno = "designateGoalsManagers";
			FacesUtil.addErrorMessage(FacesUtil.getMessageResourceString("msgFuncionalidadeNaoImplementada"));
		}
		else if (this.menuCode.equalsIgnoreCase("createTrackingRules")){
			setaTela(false, "");
			retorno = "createTrackingRules";
			FacesUtil.addErrorMessage(FacesUtil.getMessageResourceString("msgFuncionalidadeNaoImplementada"));
		}
		else if (this.menuCode.equalsIgnoreCase("createProgramActions")){
			setaTela(false, "");
			retorno = "createProgramActions";
			FacesUtil.addErrorMessage(FacesUtil.getMessageResourceString("msgFuncionalidadeNaoImplementada"));
		}
		else if (this.menuCode.equalsIgnoreCase("linkActionProcesses")){
			setaTela(false, "");
			retorno = "linkActionProcesses";
			FacesUtil.addErrorMessage(FacesUtil.getMessageResourceString("msgFuncionalidadeNaoImplementada"));
		}
		
		return retorno;
	}

	public TreeNode<TreeNodeVO> getRoot() {
		return root;
	}

	public void setRoot(TreeNode<TreeNodeVO> root) {
		this.root = root;
	}

	public void setTreeState(TreeState treeState) {
		this.treeState = treeState;
	}

	public TreeState getTreeState() {
		return treeState;
	}

	@Override
	public boolean isDirty() {		
		return false;
	}
	
	public void selectNode(NodeSelectedEvent event){
		HtmlTree tree = (HtmlTree)event.getComponent();
		Object rowKey = tree.getRowKey();
		tree.queueNodeExpand((TreeRowKey<?>) rowKey);		
	}
}
